package cn.com.surker.production.controller;

import cn.com.surker.base.BaseEntity;
import cn.com.surker.common.Code;
import cn.com.surker.enums.StateEnum;
import cn.com.surker.enums.production.invoice.InvoiceProcessEnum;
import cn.com.surker.production.entity.InvoiceHandle;
import cn.com.surker.production.entity.ProductionInvoice;
import cn.com.surker.production.entity.vo.InvoiceHandleDetail;
import cn.com.surker.production.entity.vo.ProductionInvoiceVo;
import cn.com.surker.spring.RestResultDto;
import cn.com.surker.system.model.CacheUser;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.transaction.RollbackException;
import java.util.ArrayList;
import java.util.List;

/**
 * 运输车辆相关操作
 *
 * @author luotao
 * @date 2024/9/3
 */
@RestController
@RequestMapping("/pro/invoice/handle")
public class InvoiceHandleContrller extends ProBaseController {


	/**
	 * 发货单的处理记录
	 *
	 * @param startTime
	 * @param endTime
	 * @param operateType 处理类型
	 * @param searchVal
	 * @param pageIndex
	 * @param limit
	 * @return
	 */
	@RequestMapping(value = "/page", method = RequestMethod.POST)
	@ResponseBody
	public RestResultDto tranPage(String startTime, String endTime, Integer operateType, String searchVal, Integer pageIndex, Integer limit) {
		if (pageIndex == null) {
			pageIndex = 1;
		}
		if (limit == null) {
			limit = 50;
		}
		IPage<InvoiceHandleDetail> page = invoiceHandleService.findPage(erpDb, operateType, startTime, endTime, searchVal, pageIndex, limit);
		return RestResultDto.success(page);
	}


	/**
	 * 作废操作记录
	 *
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/scrap/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	@Transactional(rollbackFor = RollbackException.class, propagation = Propagation.REQUIRED)
	public RestResultDto scrap(@PathVariable Long id, String remark) {
		if (id == null) {
			return RestResultDto.fail(Code.PARAM_EMPTY);
		}
		//	查询订单数据
		InvoiceHandle invoiceHandle = invoiceHandleService.getById(id);
		if (!BaseEntity.validateExist(invoiceHandle)) {
			return RestResultDto.fail(Code.NO_DATA);
		}
		CacheUser user = getUser();
		invoiceHandle.setUpdator(user.getName());
		invoiceHandle.addRemark("作废操作:" + remark);
		invoiceHandle.setStatus(StateEnum.DISABLED.getCode());
		//	更新数据
		invoiceHandleService.updateById(invoiceHandle);
		//	如果存在转料的目标发货单,删除目标发货单
		/*Long targetInvoiceId = invoiceHandle.getTargetInvoiceId();
		if (targetInvoiceId != null) {
			ProductionInvoice productionInvoice = invoiceService.getById(targetInvoiceId);
			if (productionInvoice != null) {
				productionInvoice.setStatus(StateEnum.DISABLED.getCode());
				invoiceService.updateById(productionInvoice);
			}
		}*/
		return RestResultDto.success();
	}

}
